//
//  RETabBarViewController.m
//  录音
//
//  Created by ywy on 2016/12/22.
//  Copyright © 2016年 Wely. All rights reserved.
//

#import "RETabBarViewController.h"
#import "RENavigationViewController.h"
#import "FirstViewController.h"// 开始首页
#import "MylibraryViewController.h" // 我的曲库
#import "RELocalViewController.h" // 本地
#import "PersonalCenterViewController.h"// 个人中心


@interface RETabBarViewController ()

@end

@implementation RETabBarViewController

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
   self.bottomView.alpha = 1;
    self.tabBar.hidden = YES;
    self.view.backgroundColor = [UIColor whiteColor];
  
}

- (void)viewDidLoad {
    [super viewDidLoad];
     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(popVC)   name:@"pop" object:nil];
    [self setChildrenVC];
    [self creatCustomTab];
}
-(void)popVC
{
    
    self.selectedIndex = 1;
    self.bottomView.alpha = 1;
    self.tabBar.hidden = YES;
    UIButton * btn  = (UIButton*)[self.view viewWithTag:202];
    btn.selected = YES;
    UIButton * firstbtn  = (UIButton*)[self.view viewWithTag:201];
    firstbtn.selected = NO;
    [self performSelector:@selector(applictionWithNotifitionPushController) withObject:self afterDelay:0.5];
}
-(void)applictionWithNotifitionPushController
{
    [[NSNotificationCenter defaultCenter] postNotificationName:@"popTwo" object:nil ];
}
-(void)setChildrenVC{
    [self setupChildVc:[[FirstViewController alloc]init] navTitle:@"听曲"];
    [self setupChildVc:[[MylibraryViewController alloc]init] navTitle:@"我的曲库"];
    [self setupChildVc:[[PersonalCenterViewController alloc]init] navTitle:@"个人中心"];
}

- (void)setupChildVc:(UIViewController *)vc navTitle:(NSString *)navTitle
{
    // 添加子控制器
    RENavigationViewController  *nav = [[RENavigationViewController alloc] initWithRootViewController:vc];
    [self addChildViewController:nav];
    UIImageView * firstImageView  =[[UIImageView alloc] initWithImage:[UIImage imageNamed:navTitle]];
    firstImageView.frame = CGRectMake(0, 0, 37, 17);
    vc.navigationItem.titleView=firstImageView;
}

-(void)creatCustomTab
{
    self.tabBar.hidden = YES;
    self.bottomView = [[UIView alloc] initWithFrame:CGRectMake(0, REScreenH-100, REScreenW, 100)];
    self.bottomView.tag = 101;
    [self.view addSubview:self.bottomView];
    CGFloat space = (REScreenW-249)/4;
    for (NSInteger i = 0; i<=2; i++) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(space+(space+83)*i, 0, 83, 94);
        // 未选中
        [button setBackgroundImage:[[UIImage imageNamed:[NSString stringWithFormat:@"tabT_%ld",i]] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
        //选中
        [button setBackgroundImage:[[UIImage imageNamed:[NSString stringWithFormat:@"tabT_s%ld",i]] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateSelected];
        button.tag = 201+i;
        [button addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        [self.bottomView addSubview:button];
    }
    UIButton *button = (UIButton *)[self.view viewWithTag:201];
    button.selected = YES;//第0个按钮默认选中
}
/**
 *  显示或隐藏TabBar
 *
 *  @param isHideen 是否隐藏
 */
- (void)HideTabarView:(BOOL)isHideen 
{
    
    if (isHideen == YES) {
//        self.tabBar.hidden = YES;
        self.bottomView.hidden = YES;
          self.tabBar.hidden = YES;
        self.bottomView.alpha = 0;
    }else if(isHideen == NO){
        self.bottomView.hidden = NO;
        self.bottomView.alpha = 1;
        self.tabBar.hidden = YES;
    }
}

- (void)btnClick:(UIButton *)button {
    //通过button 修改 tabBarController的selectedIndex 就可以切换界面
    NSInteger index = button.tag-201;
    self.selectedIndex = index;
    button.selected = YES;//设置选中
    //让其他按钮设置 非选中
    for (UIView *view in self.bottomView.subviews) {
        
        if ([view isKindOfClass:[UIButton class]]) {
            //判断是否是uibutton
            if (view.tag != button.tag) {
                UIButton *newButton = (UIButton *)view;
                newButton.selected =  NO;
            }
        }
    }
}
-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    self.bottomView.hidden = YES;
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
